package com.itheima.dao.system;


import com.itheima.domain.system.Dept;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface DeptDao  {
    /**
     * 查询所有部门
     * @return
     * @throws Exception
     */
    @Select("select * from ss_dept")
    @Results(id="deptResults" ,value = {
            @Result(property = "id",column = "dept_id",jdbcType = JdbcType.VARCHAR ),
            @Result(property = "deptName",column = "dept_name",jdbcType = JdbcType.VARCHAR ),
            @Result(property = "parentId",column = "parent_id",jdbcType = JdbcType.VARCHAR ),
            @Result(property = "state",column = "state",jdbcType = JdbcType.DECIMAL ),
            @Result(
                    property = "parent",
                    column = "parent_id",
                    javaType = Dept.class,
                    one=@One(select = "com.itheima.dao.system.DeptDao.findById")
            )
    })
    List<Dept> findAll() throws Exception;

    /**
     * 按照ID查询部门
     * @param id
     * @return
     * @throws Exception
     */
    @Select("select * from ss_dept where dept_id=#{id}")
    @ResultMap("deptResults")
    Dept findById(String id) throws Exception;

    /**
     * 新增
     * @param dept
     * @return
     * @throws Exception
     */
    @Insert("INSERT INTO `ss_dept` ( `dept_id`, `dept_name`, `parent_id`, `state` ) VALUES ( #{id},#{deptName},#{parentId},#{state} )")
    int save(Dept dept) throws Exception;

    /**
     * 修改
     * @param dept
     * @return
     * @throws Exception
     */
    @Update("UPDATE `ss_dept` SET `parent_id` = #{parentId}, `state` = #{state} WHERE `dept_id` = #{id}")
    int update(Dept dept) throws Exception;

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @Delete("delete from ss_dept where dept_id = #{deptId}")
    int delete(String id) throws Exception;


    @Select("select * from ss_dept where dept_name like #{deptName}")
    @ResultMap("deptResults")
    List<Dept> search(String deptName) throws Exception;
}
